package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Article;
import com.example.exception.FloraException;
import com.example.service.ArticleService;
import com.example.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;



    @GetMapping("/getArticleByPlantCategoryId/{id}")
    public ResponseResult getArticleByPlantCategoryId(@PathVariable Long id){
       return ResponseResult.okResult(articleService.getArticleByPlantCategoryId(id));
    }

    @GetMapping("/browseList")
    public ResponseResult browseList(){
        System.out.println(articleService.getBrowseList());
        return ResponseResult.okResult(articleService.getBrowseList());

    }
    @GetMapping("/{id}")
    public ResponseResult getArticle(@PathVariable Long id){
        return ResponseResult.okResult(articleService.getArticleById(id));
    }

    @GetMapping("/baike")
    public ResponseResult latestBaike(){
        System.out.println(articleService.getLatestBaikeArticle());
        return ResponseResult.okResult(articleService.getLatestBaikeArticle());
    }

    /** 首页显示的文章page（显示时要显示 标题、缩略图、概述、分类、种类 等，具体正文内容要点进去才能看到） */
    /** 可以设置下拉框选择 文章分类（杀虫、除草...）文章类型（分享、求助） */
    /** 可以查看该文章的关联植物（如果有的话）*/
    /** 可以查看某个植物的关联帖子 */
    @GetMapping("/getArticlePage")
    public IPage<Article> getArticlePage(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String categoryName,
            @RequestParam(required = false) String typeName,
            @RequestParam(required = false) Long plantId,
            @RequestParam(required = false,defaultValue = "0") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5") Integer pageSize
    ){
        Map<String,Object> condition = new HashMap<>();
        if(title!=null){
            condition.put("title",title);
        }
        if(categoryName!=null){
            condition.put("categoryName",categoryName);
        }
        if(typeName!=null){
            condition.put("typeName",typeName);
        }
        if(plantId!=null){
            condition.put("plantId",plantId);
        }
        return articleService.getArticlePage(condition,pageNum,pageSize);
    }

    /** 点击阅读全文，增加浏览量到redis中，redis每十分钟把数据同步到数据库
     *  */
    @PutMapping("updateViewCount/{id}")
    public void updateArticleViewCount(@PathVariable Long id){
        articleService.updateArticleViewCount(id);
    }

    /** 查看个人草稿箱 */
    @GetMapping("/getDraftPage")
    public IPage<Article> getDraftPage(@RequestParam Long userId,
                                       @RequestParam(required = false,defaultValue = "0") Integer pageNum,
                                       @RequestParam(required = false,defaultValue = "5") Integer pageSize){
        return articleService.getDraftPage(userId,pageNum,pageSize);
    }

    /** 删除文章 */
    @DeleteMapping("/delete")
    public void deleteArticle(@RequestParam Long id) throws FloraException {
        articleService.deleteArticle(id);
    }

    /** 修改文章（修改文章内容（也可以修改草稿箱里的内容） 或者 将草稿文章发布）
     * 进行了合法性验证 */
    @PutMapping("/updateArticle")
    public void updateArticle(@RequestBody Article article) throws FloraException {
        articleService.updateArticle(article);
    }

    /** 添加文章
     * 进行了合法性验证 */
    @PostMapping("/addArticle")
    public ResponseResult addArticle(@RequestBody Article article) throws FloraException {
        return ResponseResult.okResult(articleService.addArticle(article));
    }

    /** 点赞文章 */
    @PutMapping("/updatelikeCount/{articleId}")
    public void updateLikeCount(@PathVariable Long articleId) throws FloraException {
        articleService.updateLikeCount(articleId);
    }
}
